package edu.stanford.hardon.Tabs;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import edu.stanford.hardon.R;
import edu.stanford.hardon.Media.Media;
import edu.stanford.hardon.Media.MediaType;
import edu.stanford.hardon.Media.RichMedia;
import edu.stanford.hardon.Media.TextMedia;

public class ManageAnnotationListAdapter extends BaseAdapter implements Filterable{

	private Context context;
	private LayoutInflater layInflater;
	private List<Media> displayItems;
	private ManageTab mt;
	private MediaPlayer mp;
	
	public ManageAnnotationListAdapter(Context context, ManageTab parent){
		layInflater = LayoutInflater.from(context);
		this.context = context;
		mt = parent;
		displayItems = new ArrayList<Media>();
		mp = null;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		
		return displayItems.size();
	}
	
	public void setItems(List<Media> items){
		displayItems = items;

	}
	
	public void addItem(Media m){
		displayItems.add(m);
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ManageListViewHolder holder;
		
		if(convertView == null){
			convertView = layInflater.inflate(R.layout.listview_adaptor_main, null);
			holder = new ManageListViewHolder();
			holder.time = (TextView) convertView.findViewById(R.id.mmListTime);
			holder.type = (ImageView) convertView.findViewById(R.id.mmListType);
			holder.content = (TextView) convertView.findViewById(R.id.mmListContent);
			holder.contentImg = (ImageView) convertView.findViewById(R.id.mmListContentImg);
			convertView.setTag(holder);
		}
		else{
			holder = (ManageListViewHolder) convertView.getTag();
		}
		final Media m = displayItems.get(position);
		switch(m.getType()){
		case PHOTO:{
			holder.type.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.camera));
			holder.content.setVisibility(View.GONE);
			holder.contentImg.setVisibility(View.VISIBLE);
			Log.i("PATH", ((RichMedia)m).getPath());
			File photo = new File(((RichMedia)m).getPath());
			if(photo.exists()){
				holder.contentImg.setImageURI(Uri.parse(photo.toString()));
			}
		
			break;
		}
		case VIDEO:{
			holder.type.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.video));
			holder.content.setVisibility(View.GONE);
			holder.contentImg.setVisibility(View.VISIBLE);
			
			break;
		}
		case AUDIO:{
			holder.type.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.microphone));
			holder.content.setVisibility(View.GONE);
			holder.contentImg.setVisibility(View.GONE);
		
			break;
		}
		case NOTE:{
			holder.type.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.notes));
			holder.contentImg.setVisibility(View.GONE);
			holder.content.setVisibility(View.VISIBLE);
			holder.content.setText(((TextMedia)m).getContent());
			
			break;
		}
		default:{
			break;
		}
		}
		
		// What are we trying to do here?  Check out the getTimeNow() method in GenericTab.java.  -Letitia
		String createTime = "";
//		Date createDate = m.getTimeCreated();
//		Timestamp ts = new Timestamp(createDate.getTime());
		Date createDate = new Date(m.getTimeCaptured().toString());
//		createTime = "5:50";

		createTime += String.valueOf(createDate.getHours()) + ":" ;
		if(createDate.getMinutes() < 10){
			createTime += "0" + String.valueOf(createDate.getMinutes()) + ", ";
		}
		else{
			createTime += String.valueOf(createDate.getMinutes()) + ", ";
		}
		createTime += String.valueOf(createDate.getMonth()) + "/";
		createTime += String.valueOf(createDate.getDate()) + "/";
		createTime += String.valueOf(createDate.getYear() + 1900);
		holder.time.setText(createTime);
		
		
		convertView.setOnClickListener(new OnClickListener(){
			protected int pos = position;
			protected MediaType state = m.getType();
			public void onClick(View v) {
				if(state != MediaType.AUDIO){
//					mt.switchState(getTabState(state),displayItems.get(pos));
				}
				else{
					if(mp == null){
						File audio = new File(((RichMedia)displayItems.get(pos)).getPath());
						if(audio.exists()){
							mp = MediaPlayer.create(context, Uri.parse(audio.toString()));
							mp.setOnCompletionListener( new OnCompletionListener(){
							
								public void onCompletion(MediaPlayer mp2) {
									if(mp!= null){
										mp.stop();
										mp.seekTo(0);
										mp.release();
										mp = null;
									}

								}
									
								
							});
							mp.start();
						}
					
					}
					else{
						Log.i("MP","mp is not null");
					}
				}
//				Log.i("CLICK","click on position " + pos );
						
			}
			
			public MediaTabState getTabState(MediaType state){
				switch(state){
				case PHOTO:{
					return MediaTabState.PHOTO_VIEW;
				}
				case VIDEO:{
					return MediaTabState.VIDEO_VIEW;
				}
				case AUDIO:{
					return MediaTabState.AUDIO_VIEW;
				}
				case NOTE:{
					return MediaTabState.NOTE_VIEW;
				}
				default:{
					return MediaTabState.MAIN;
				}
				}
			}
			
		});
		
		return convertView;
	}

	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

}
